package com.yidu.controller;

import com.yidu.entity.LvTb;
import com.yidu.service.LvTbService;
import com.yidu.utils.BaseMessage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (LvTb)表控制层
 *
 * @author JiuYue
 * @since 2021-07-29 16:28:36
 */
@RestController
@RequestMapping("lvTb")
public class LvTbController {
    /**
     * 服务对象
     */
    @Resource
    private LvTbService lvTbService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("selectOne")
    public BaseMessage selectOne(Integer id) {
        LvTb lvTb=this.lvTbService.queryById(id);
        return BaseMessage.YES("查询成功",lvTb) ;
    }

    /**
     * 显示列表
     * @param lvTb
     * @return
     */
    @GetMapping("/TypeList")
    public Map<String, Object> showList(LvTb lvTb){
        List<LvTb>list=lvTbService.queryAllByLimit(lvTb);
        Map<String,Object>map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",lvTbService.findCount(lvTb));
        map.put("data",list);
        return map;
    }
    @GetMapping("/flowerTypeList")
    public Map<String, Object> typeList(LvTb lvTb){
        List<LvTb>list=lvTbService.queryAllByLimit(lvTb);
        Map<String,Object>map=new HashMap<>();
        map.put("data",list);
        return map;
    }

    /**
     * 增加和修改
     * @param lvTb
     * @return
     */
    @PostMapping("addOrUpdate")
    public BaseMessage addOrUpdate(LvTb lvTb){
        LvTb po=null;
        System.out.println("111111111111111111111111111111111111111111111111"+lvTb);
        if (lvTb.getLid()==null){
            po=lvTbService.insert(lvTb);
        }else{
            po=lvTbService.update(lvTb);
        }
        if (po==null){
            return BaseMessage.NO("保存失败");
        }else{
            return BaseMessage.YES("保存成功");
        }
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @PostMapping("delete")
    public BaseMessage delete(Integer id){
        Boolean flag=lvTbService.deleteById(id);
        return flag?BaseMessage.YES("操作成功"):BaseMessage.NO("操作失败");
    }

    @PostMapping("deleteBatch")
    public BaseMessage deleteBatch(String ids){
        String[] id=ids.split(",");
        int row=lvTbService.deleteByIds(id);
        return row>0?BaseMessage.YES("删除成功"):BaseMessage.NO("删除失败");
    }
}
